package com.ruoyi.system.handler;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;

@MappedJdbcTypes(JdbcType.OTHER)
@MappedTypes(List.class)
public class JsonbListListBigDecimalTypeHandler extends BaseTypeHandler<List<List<BigDecimal>>> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, List<List<BigDecimal>> parameter, JdbcType jdbcType)
			throws SQLException {
		try {
			String json = JSON.toJSONString(parameter);
			ps.setObject(i, json, java.sql.Types.OTHER);
		} catch (Exception e) {
			throw new SQLException("设置 jsonb 参数失败", e);
		}

	}

	@Override
	public List<List<BigDecimal>> getNullableResult(ResultSet rs, String columnName) throws SQLException {
		String text = rs.getString(columnName);
		return StringUtils.isBlank(text) ? null : JSON.parseObject(text,new TypeReference<List<List<BigDecimal>>>(){});
	}

	@Override
	public List<List<BigDecimal>> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		String text = rs.getString(columnIndex);
		return StringUtils.isBlank(text) ? null : JSON.parseObject(text,new TypeReference<List<List<BigDecimal>>>(){});
	}

	@Override
	public List<List<BigDecimal>> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		String text = cs.getString(columnIndex);
		return StringUtils.isBlank(text) ? null : JSON.parseObject(text,new TypeReference<List<List<BigDecimal>>>(){});
	}
}
